package com.yq.light.feedback.code.controller;

import com.yq.light.commons.code.entity.CommonResult;
import com.yq.light.commons.code.entity.Feedback;
import com.yq.light.feedback.code.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/feedback")
@Slf4j
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * select feedback list
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public CommonResult feedbackList(){
        try{
            List<Feedback> list = feedbackService.feedbackList();
            if(list.isEmpty()){
                return new CommonResult(200,"请求成功","暂无数据");
            }else{
                return new CommonResult(200,"请求成功",list);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new CommonResult(200,"数据获取失败","错误");
    }

    @RequestMapping("/newFeedback")
    @ResponseBody
    public CommonResult newFeedback(@RequestBody Feedback feedback){
        log.info("用户新建反馈 info --",feedback);
        Date da = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp date = Timestamp.valueOf(sdf.format(da));
        feedback.setFbTime(date);
        try{
            int result = feedbackService.newFeedback(feedback);
            if(result > 0){
                return new CommonResult(200,"请求成功",result);
            }else{
                return new CommonResult(200,"请求成功","未成功执行");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new CommonResult(200,"请求失败","错误");
    }
}
